##last modified date: 2015.3.28

import collectd
import subprocess

interval = 60

##read cpu parameter
def read_cpu():
	global interval
	plugin_name = 'cpu'
	try:
		#read stdout from pipe
		p = subprocess.Popen('vmstat',stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)
		stdout = ''
		while True:
			line = p.stdout.readline()
			if line == '':
				break
			stdout+=line

		#parse stdout
		lines = stdout.split('\n')
		metric_arr = lines[1].split()
		value_arr = lines[2].split()
		para_dict = {}
		for i in range(0,len(metric_arr)):
			para_dict[metric_arr[i]] = int(value_arr[i])
			
		#insert into collectd
		cpu_user = collectd.Values(type='cpu_user',plugin=plugin_name,values=[para_dict['us']], interval=interval)
		cpu_system = collectd.Values(type='cpu_system',plugin=plugin_name,values=[para_dict['sy']], interval=interval)
		cpu_idle = collectd.Values(type='cpu_idle',plugin=plugin_name,values=[para_dict['id']])
		cpu_ratio = collectd.Values(type='cpu_ratio',plugin=plugin_name,values=[(100-para_dict['id'])*1.0/100], interval=interval)
		cpu_user.dispatch()
		cpu_system.dispatch()
		cpu_idle.dispatch()
		cpu_ratio.dispatch()
	except Exception as e:
		print e
		
##read memory parametor
def read_memory():
	global interval
	plugin_name = 'memory'
	try:
		#read stdout from pipe
		p = subprocess.Popen('vmstat',stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)
		stdout = ''
		while True:
			line = p.stdout.readline()
			if line == '':
				break
			stdout+=line

		#parse stdout
		lines = stdout.split('\n')
		metric_arr = lines[1].split()
		value_arr = lines[2].split()
		para_dict = {}
		for i in range(0,len(metric_arr)):
			para_dict[metric_arr[i]] = int(value_arr[i])
			
		#insert into collectd

		cpu_swpd = collectd.Values(type='memory_swpd',plugin=plugin_name,values=[para_dict['swpd']],interval=interval)
		cpu_free = collectd.Values(type='memory_free',plugin=plugin_name,values=[para_dict['free']],interval=interval)
		cpu_buff = collectd.Values(type='memory_buff',plugin=plugin_name,values=[para_dict['buff']],interval=interval)
		cpu_cache = collectd.Values(type='memory_cache',plugin=plugin_name,values=[para_dict['cache']],interval=interval)
		total = para_dict['swpd']+para_dict['free']+para_dict['buff']+para_dict['cache']
		ratio = (total-para_dict['free'])*1.0/total
		cpu_total = collectd.Values(type='memory_total',plugin=plugin_name,values=[para_dict['cache']],interval=interval)		
		cpu_ratio = collectd.Values(type='memory_ratio',plugin=plugin_name,values=[ratio],interval=interval)
		
		cpu_swpd.dispatch()
		cpu_free.dispatch()
		cpu_buff.dispatch()
		cpu_cache.dispatch()
		cpu_total.dispatch()
		cpu_ratio.dispatch()
	except Exception as e:
		print e

##read disk parameter
def read_disk():
	global interval
	plugin_name = 'disk'
	try:
		#read stdout from pip
		p = subprocess.Popen('df',stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)
		stdout = ''
		while True:
			line = p.stdout.readline()
			if line == '':
				break
			stdout+=line
		
		#parse stdout
		disks_array = stdout.split('\n')
		for i in range(1, len(disks_array)-1):
			arr = disks_array[i].split()
			filesystem = arr[0]
			total = int(arr[1])
			used = int(arr[2])
			available = int(arr[3])
			ratio = int(arr[4].split("%")[0])*1.0/100
			
			#mount == /
			if len(arr)==4:
				mount='/'
			else:
				mount = arr[5]
			
			
			if 'vd' not in filesystem:
				continue
			
			#insert into collectd
			vd = filesystem.split('/')
			tmp_len = len(vd)

			plugin_instance = vd[tmp_len-1]
			disk_total = collectd.Values(type='disk_total',plugin=plugin_name, plugin_instance=plugin_instance,values=[total],interval=interval)
			disk_used = collectd.Values(type='disk_used',plugin=plugin_name, plugin_instance=plugin_instance,values=[used],interval=interval)
			disk_available = collectd.Values(type='disk_available',plugin=plugin_name, plugin_instance=plugin_instance,values=[available],interval=interval)
			disk_ratio = collectd.Values(type='disk_ratio',plugin=plugin_name, plugin_instance=plugin_instance,values=[ratio],interval=interval)
			
			#remove 0.0 ratio disk
			if ratio<=0.01:
				continue
			
			disk_total.dispatch()
			disk_used.dispatch()
			disk_available.dispatch()
			disk_ratio.dispatch()
	except Exception as e:
		print e

def config(c):
	global interval
		
	if c is None:
		pass
    
	for child in c.children:
		if child.key == 'INTERVAL':
			interval = child.values[0]

	pass
	
def init():
	pass

			
def read(data=None):
	read_memory()
	read_cpu()
	read_disk()

def write(v, data=None):
	print v
		

def shutdown():
	pass
collectd.register_init(init)
collectd.register_config(config)
collectd.register_read(read)
collectd.register_write(write)
collectd.register_shutdown(shutdown)